<template>
  <div>
    <!-- 新增编辑的弹层 -->
    <el-dialog :visible="showDialog" :title="showtitle" @close="closefn">
      <!-- 表单内容 -->
      <el-form ref="addform" label-width="100px" :model="formData" :rules="rules">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="formData.description" />
        </el-form-item>
        <el-form-item label="权限启用">
          <el-switch
            v-model="formData.enVisible"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>

      <template #footer>
        <div style="text-align: right;">
          <el-button @click="closefn">取消</el-button>
          <el-button type="primary" @click="addPrenfn">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { addPremissAPI, getPermissionDetailApi, updatePermissionApi } from '@/api/permission'
export default {
  name: 'AddPremiss',
  props: {
    showDialog: {
      type: Boolean
    }
  },
  data() {
    return {
      list: [],
      formData: {
        enVisible: '0', // 启用禁用, 0禁用, 1启用
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型标记了一级(页面访问权) 二级(按钮操作权)
        pid: '' // 添加到哪个节点下
      },
      rules: {
        name: [
          { required: true, message: '名称不能为空', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '权限标识不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '描述不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    showtitle() {
      return this.formData.id ? '编辑功能' : '添加功能'
    }
  },
  methods: {
    /**
     * 获取权限详情
     */
    async getPermissionDetail(id) {
      const res = await getPermissionDetailApi(id)
      console.log(res)
      this.formData = res.data
    },
    closefn() {
      this.$emit('update:showDialog', false)
      this.$refs.addform.resetFields()
      this.formData = {
        enVisible: '0', // 启用禁用, 0禁用, 1启用
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型标记了一级(页面访问权) 二级(按钮操作权)
        pid: '' // 添加到哪个节点下
      }
    },
    /**
     * 添加功能
     */
    addPrenfn() {
      this.$refs.addform.validate(async(flag) => {
        if (flag) {
          if (this.formData.id) {
            const res = await updatePermissionApi(this.formData)
            console.log(res)
            this.$message.success('编辑成功')
          } else {
            const res = await addPremissAPI(this.formData)
            console.log(res)
            this.$message.success('添加成功')
          }

          this.closefn()
          this.$parent.userList()
        }
      })
      // if (this.formList.id === '0') {
      //   this.formData.pid = this.formList.id
      //   this.formData.type = this.formList.type
      //   const res = await addPremissAPI(this.formData)
      //   console.log(res)
      //   this.$message.success('添加成功')
      // // } else {
      // //   // this.formData.pid = this.formList.id
      // //   // this.formData.type = this.formList.type
      // //   const res = await addPremissAPI(this.formData)
      // //   console.log(res)
      // //   this.$message.success('添加子权限成功')
      // // }
    }
  }
}
</script>

<style>

</style>
